// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.components;

import java.io.File;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ComponentIconsLoading {

    private final File folder;

    private ImageDescriptor image32;

    private ImageDescriptor image24;

    private ImageDescriptor image16;

    /**
     * Qiang.Zhang.Adolf@gmail.com ComponentIconsLoading constructor comment.
     * 
     * @param folder
     */
    public ComponentIconsLoading(File folder) {
        this.folder = folder;
    }

    public ImageDescriptor getImage16() {
        if (image16 == null) {
            String icon16FileName = ComponentFilesNaming.getInstance().getIcon16FileName(folder.getName());
            File file = new File(folder, icon16FileName);
            if (file.exists()) {
                image16 = getImage(icon16FileName);
            } else {
                image16 = getImage32();
            }
        }

        return this.image16;
    }

    public ImageDescriptor getImage24() {
        if (image24 == null) {
            String icon24FileName = ComponentFilesNaming.getInstance().getIcon24FileName(folder.getName());
            File file = new File(folder, icon24FileName);
            if (file.exists()) {
                image24 = getImage(icon24FileName);
            } else {
                image24 = getImage32();
            }
        }

        return this.image24;
    }

    public ImageDescriptor getImage32() {
        if (image32 == null) {
            String icon32FileName = ComponentFilesNaming.getInstance().getIcon32FileName(folder.getName());
            image32 = getImage(icon32FileName);
        }
        return this.image32;
    }

    private ImageDescriptor getImage(String fileName) {
        try {
            URL url = new URL(folder.toURI().toURL() + fileName);
            return ImageDescriptor.createFromURL(url);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
